package com.mrd.gtimp.business.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.config.web.json.annotation.JField;
import com.mrd.framework.config.web.json.annotation.Json;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.business.entity.MsgBox;
import com.mrd.gtimp.business.entity.MsgBoxList;
import com.mrd.gtimp.business.service.MsgBoxBiz;
import com.mrd.gtimp.business.service.MsgBoxListBiz;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2016/10/11.
 */
@Controller
@RequestMapping("/msgBox")
public class MsgBoxAction {

    @Resource
    private MsgBoxBiz msgBoxBiz;

    @Resource
    private MsgBoxListBiz msgBoxListBiz;

    /**
     * 消息配置主页
     */
    @RequestMapping("/main")
    public String main(Model model) {
        try {
            List<MsgBox> msgBoxList = msgBoxBiz.findAllEntity();
            model.addAttribute("msgBoxList", msgBoxList);
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return "gtimp3/msgBox/main";
    }

    /**
     * 消息配置修改页
     */
    @RequestMapping("/edit")
    public String edit(Model model, String id) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw new Exception("MsgBoxAction.edit error!id=" + id);
            }
            MsgBox msgBox = msgBoxBiz.findEntityById(id);
            model.addAttribute("msgBox", msgBox);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "gtimp3/msgBox/edit";
    }

    /**
     * 消息配置保存
     */
    @RequestMapping("/doSave")
    @ResponseBody
    public Result doSave(MsgBox msgBox) {
        try {
            if (msgBox != null && StringUtils.isNotEmpty(msgBox.getId())) {
                msgBoxBiz.saveOrupdateEntity(msgBox);
                return Result.newFailInstance(MsgConstants.SAVE_SUECCES);
            } else {
                throw new Exception("MsgBoxAction.doSave error!id=" + msgBox.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.newFailInstance(MsgConstants.SAVE_FAIL);
    }

    /**
     * 消息列表
     */
    @RequestMapping("/msgList")
    public String msgList() {
        return "gtimp3/msgBox/list";
    }

    @RequestMapping("/findMsgBoxList")
    @ResponseBody
    @Json({@JField("id"), @JField("title"), @JField("createTime")})
    public Page<MsgBoxList> findMsgBoxList(Jqgrid jqgrid) {
        try {
            User currentUser = LoginUtil.getLoginUser();
            jqgrid.addRule(new SearchRule("recipient", SearchRuleOption.eq, currentUser));
            jqgrid.addOrder(Sort.Direction.DESC, "isRead");
            return msgBoxListBiz.findByJqgrid(jqgrid);
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/view")
    public String view(Model model, String id) {
        if (StringUtils.isNotEmpty(id)) {
            try {
                MsgBoxList msgBoxList = msgBoxListBiz.findEntityById(id);
                model.addAttribute("msgBoxList", msgBoxList);
            } catch (QueryException e) {
                e.printStackTrace();
            }
        }
        return "gtimp3/msgBox/view";
    }
}
